#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    :views.py
@Author  :keyin
@Time    :2021-02-19 12:36
"""
from sqlalchemy import exists

from apps.card import models
from utils.AipOcr import CardAip

from utils import ViewModel
from utils.genJsonResponse import json_response
from utils.res_code import error_map, Code


class CardDataView(ViewModel.BasicView):
    """
    返回数据表中的需要的数据
    """

    def get(self, a, b):
        """

        :param a: 起始位置
        :param b: 结束位置
        :return: json格式查询到的数据
        """
        res = self.session.query(models.CardData).order_by(models.CardData.c_id.desc())[a:b]
        data = {
            'res': res
        }

        return json_response(data=data)

    def get_select(self, a, b):
        res = self.session.query(models.CardData.c_name,
                                 models.CardData.c_mobile,
                                 models.CardData.c_idNumber,
                                 ).order_by(models.CardData.c_id.desc())[a:b]
        data = {
            'res': res
        }

        return json_response(data=data)

    def get_idNumber(self, idNumber):
        res = self.session.query(models.CardData).filter(models.CardData.c_idNumber == idNumber).first()
        data = {
            'res': res
        }

        return json_response(data=data)

    def save(self, data):

        data = data['data']
        if self.session.query(exists().where(models.CardData.c_idNumber == data['c_idNumber'])).scalar():
            return json_response(errno=Code.DATAEXIST, errmsg=error_map[Code.DATAEXIST])
        else:
            print('*' * 10, data)
            spi_data = models.CardData()
            spi_data.c_name = data['c_name']
            spi_data.c_sex = data['c_sex']
            spi_data.c_nation = data['c_nation']
            spi_data.c_idNumber = data['c_idNumber']
            spi_data.c_address = data['c_address']
            spi_data.c_mobile = data['c_mobile']
            spi_data.c_checkPhone = data['c_checkPhone']
            spi_data.c_wechat = data['c_wechat']
            spi_data.c_em = data['c_em']
            spi_data.c_valStart = data['c_valStart']
            spi_data.c_valEnd = data['c_valEnd']
            self.session.add(spi_data)
            self.session.commit()
            self.session.close()
            return json_response(errno=Code.OK)
            # spi_data.c_nation

            # try:
            #     self.session.add(spi_data)
            #     self.session.commit()
            #     return json_response(Code.OK)
            # except Exception as e:
            #     return json_response(errno=Code.UNKOWNERR, errmsg='{}'.format(e))

    def updata(self, data):
        data = data['data']
        print(type(data), data)
        for item in list(data.keys()):
            if item in data:
                if data[item] == '' or data[item] is None:
                    data.pop(item)
            if 'c_em' in data:
                if data['c_em'] == -1:
                    data.pop('c_em')
        # print(data)
        self.session.query(models.CardData).filter(models.CardData.c_idNumber == data['c_idNumber']).update(data)
        self.session.commit()
        self.session.close()

    def deltele_idNumber(self,data):
        card = self.session.query(models.CardData).filter(models.CardData.c_idNumber == data).first()
        self.session.delete(card)
        self.session.flush()
        self.session.commit()
        self.session.close()

    @staticmethod
    def get_card_info(face, filePath):
        api = CardAip(filePath)
        data = api.getinfo(face)
        # print(data)
        return json_response(data=data)


if __name__ == '__main__':
    pass
